#include <QgsSimulation/LayerTools.h>
#include <QFileInfo>
#include <qgsmapsettings.h>
#include <qgsmaprenderersequentialjob.h>
#include <qgsvectorfilewriter.h>
#include <qgsproject.h>
#include <qgssinglesymbolrenderer.h>

using SimulationCore::QgsSimulation::LayerTools;

QgsRasterLayer* LayerTools::loadRasterLayer(const QString &path_to_raster)
{
	QFileInfo fileinfo(path_to_raster);
	auto raster_layer = new QgsRasterLayer(path_to_raster, fileinfo.baseName());
	if (!raster_layer->isValid())
	{
		qDebug() << "Failed to load remote sensing image:" << raster_layer->error().message();
		delete raster_layer;
		raster_layer = nullptr;
	}
	return raster_layer;
}

QgsVectorLayer* LayerTools::loadFeatureLayer(const QString & path_to_shpfile)
{
	QFileInfo fileinfo(path_to_shpfile);
	auto feature_layer = new QgsVectorLayer(path_to_shpfile, fileinfo.baseName(), "ogr");
	if (!feature_layer->isValid())
	{
		qDebug() << "Failed to load remote sensing image:" << feature_layer->error().message();
		delete feature_layer;
		feature_layer = nullptr;
	}
	//feature_layer->setUserData
	return feature_layer;
}

QgsVectorLayer * LayerTools::loadFeatureLayer(const QString & gpkg_path, const QString & layer_name, const QString & layer_type)
{
	auto feature_layer = new QgsVectorLayer(gpkg_path + "|layername=" + layer_name, layer_name, "ogr");
	if (!feature_layer->isValid())
	{
		qDebug() << "Failed to load remote sensing image:" << feature_layer->error().message();
		delete feature_layer;
		feature_layer = nullptr;
		return feature_layer;
	}
	feature_layer->setShortName(layer_name + layer_type);
	return feature_layer;
}

QgsVectorLayer * LayerTools::createFeatureLayer(const QString & gpkg_path, const QString & layer_name, const QString & layer_type,int epsg)
{
	QgsWkbTypes::Type osf_wkb;
	if (layer_type == "POINT") {
		osf_wkb = QgsWkbTypes::Point;
	}
	else if (layer_type == "LINE") {
		osf_wkb = QgsWkbTypes::LineString;
	}
	else {
		osf_wkb = QgsWkbTypes::Polygon;
	}

	QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem(QString("EPSG:%1").arg(epsg));
	
	
	QgsVectorFileWriter::SaveVectorOptions options;
	options.driverName = "GPKG";
	options.fileEncoding = "UTF-8";
	options.layerName = layer_name;

	QFile file(gpkg_path);
	if (file.exists()) {
		options.actionOnExistingFile = QgsVectorFileWriter::CreateOrOverwriteLayer;
	}
	else {
		options.actionOnExistingFile = QgsVectorFileWriter::CreateOrOverwriteFile;
	}

	QgsFields fields;
	fields.append(QgsField("FID", QVariant::Int));
	fields.append(QgsField("SID", QVariant::String));
	fields.append(QgsField("Name", QVariant::String));
	fields.append(QgsField("Label", QVariant::String));
	fields.append(QgsField("Info", QVariant::String));
	QgsVectorFileWriter* writer = QgsVectorFileWriter::create(
		gpkg_path,
		fields, osf_wkb, crs,
		QgsProject::instance()->transformContext(), options);

	delete writer;
	writer = nullptr;

	return loadFeatureLayer(gpkg_path, layer_name, layer_name);
}


void LayerTools::setLayerSymbol(QgsVectorLayer* layer, QgsSymbol * symbol)
{
	QgsFeatureRenderer* renderer = layer->renderer();
	QgsSingleSymbolRenderer* single_renderer = dynamic_cast<QgsSingleSymbolRenderer*>(renderer);
	if (single_renderer) {
		single_renderer->setSymbol(symbol);
	}
	layer->triggerRepaint();
}

QgsSymbol * LayerTools::getLayerSymbol(QgsVectorLayer * layer)
{
	QgsSymbol *symbol = nullptr;
	QgsSingleSymbolRenderer* single_renderer = dynamic_cast<QgsSingleSymbolRenderer*>(layer->renderer());
	if (single_renderer) {
		symbol = single_renderer->symbol();
	}
	return symbol;
}

void LayerTools::exportLayer(const QString & shpfile, QgsVectorLayer * layer)
{
	QgsVectorFileWriter::SaveVectorOptions options;
	options.driverName = "ESRI Shapefile";
	options.fileEncoding = "UTF-8";
	QgsVectorFileWriter::writeAsVectorFormatV2(layer, shpfile, QgsProject::instance()->transformContext(), options);
}








